﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

// TCO13 Round 1A DIV 1
public class HouseBuilding
{
    public int getMinimum(string[] area)
    {
        string areaString = String.Join("", area);
 
        int[] heightArray = new int[areaString.Length];
        for ( int i = 0 ; i < areaString.Length ; i++ ) {
            heightArray[i] = int.Parse(areaString[i].ToString());
        }

        int minSum = -1;
        for ( int low = 0 ; low <= 9 ; low++ ) {
            int sum = 0;
            for ( int i = 0 ; i < heightArray.Length ; i++ ) {
                if ( low+1 < heightArray[i] ) {
                    sum += heightArray[i]-(low+1);
                } else if ( heightArray[i] < low ) {
                    sum += (low-heightArray[i]);
                }
            }
            if (minSum == -1 || sum < minSum) minSum = sum;
        }
        return minSum;
    }
}
